package com.fcscs.cservice.client.main.restaurants;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.core.gwt.common.ui.SimpleGridPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.restaurants.RestaurantsModelData;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class RestaurantsGrid extends SimpleGridPanel<RestaurantsModelData> {
//	private RestaurantsDetailDialog restaurantsDetailDialog;
	
	public RestaurantsGrid(String heading, ModelSelectListener<RestaurantsModelData> modelSelectListener) {
		super(heading, null, (Object[]) null);
		setCollapsible(false);
		setSize(950, 540);
	}

	@Override
	protected ColumnModel createColumnConfig(Object... params) {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		ColumnConfig cc = CoreUtil.createNonSortableColumnConf(CServiceConstants.RESTAURANT,
				CServiceConstants.CSERVICE_CONSTANTS.restaurant(), 140);
		columns.add(cc);

		cc = CoreUtil.createNonSortableColumnConf(CoreConstants.DESCRIPTION,
				CoreConstants.CORE_CONSTANTS.description(), 140);
		columns.add(cc);

		columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.BUSINESSLOCATION,
				CServiceConstants.CSERVICE_CONSTANTS.businessLocation(), 160));
		columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.DEFAULTRESPICID,
				CServiceConstants.CSERVICE_CONSTANTS.defaultRespicid(), 140));

		columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.AVERAGESPENDING,
				CServiceConstants.CSERVICE_CONSTANTS.averageSpending(), 140));
		columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.CUISINETYPEID,
				CServiceConstants.CSERVICE_CONSTANTS.cuisineTypeid(), 140));
		cc = CoreUtil.createNonSortableColumnConf(CoreConstants.ACTION,
				CoreConstants.CORE_CONSTANTS.action(), 70);
		cc.setRenderer(new GridCellRenderer<RestaurantsModelData>() {

			public Object render(final RestaurantsModelData model, String property, ColumnData config,
					int rowIndex, int colIndex, ListStore<RestaurantsModelData> store,
					final Grid<RestaurantsModelData> grid) {
				HorizontalPanel hr = new HorizontalPanel();
				hr.setHorizontalAlign(HorizontalAlignment.LEFT);
				HighLightIconButton ib = createEditBtn(model);
				ib.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "3px");
				hr.add(ib);
				return hr;
			}
		});
		columns.add(cc);
		ColumnModel columnModel = new ColumnModel(columns);

		return columnModel;
	}
	
	private HighLightIconButton createEditBtn(final RestaurantsModelData model) {
		HighLightIconButton iconBtn = new HighLightIconButton(ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
//						showEditDialog(model);
					}
				});
		iconBtn.setTitle(CoreConstants.CORE_CONSTANTS.edit());
		return iconBtn;
	}	
	
//	private void showEditDialog(RestaurantsModelData model) {
//		restaurantsDetailDialog=RestaurantsDetailDialog.getInstance();
//		restaurantsDetailDialog.show();
//	}
}
